package com.example.itcacp.repository.mybatis;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.itcacp.entity.Category;
import com.example.itcacp.entity.Project;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ProjectMapper extends BaseMapper<Project> {


    @Select ( "SELECT * FROM itcacp.cd_project " +
            "WHERE user_id = #{userId} " +
            "AND project_name = #{projectName}" )
    Project selectProject (Long userId, String projectName);

    @Select ( "SELECT * FROM itcacp.cd_project WHERE project_id = #{projectId}" )
    @Results({
            @Result(property = "projectId", column = "project_id"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "tags", column = "tags"),
            @Result(property = "user", column = "user_id", javaType = String.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.UserMapper.findBolgUserById")),
            @Result(property = "category", column = "category_id", javaType = Category.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.ContentMapper.selectCategoryById")),
            @Result(property = "collections", column = "project_id", javaType = Long.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.CollectMapper.findCollectByProjectId")),
            @Result(property = "views", column = "project_id", javaType = Long.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.BrowseHistoryMapper.findBrowseHistoryByProjectId")),
    })
    Project selectProjectById (Long projectId);



    @Select ( "SELECT * FROM cd_project ORDER BY created_at DESC" )
    @Results({
            @Result(property = "projectId", column = "project_id"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "tags", column = "tags"),
            @Result(property = "user", column = "user_id", javaType = String.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.UserMapper.findBolgUserById")),
            @Result(property = "category", column = "category_id", javaType = Category.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.ContentMapper.selectCategoryById")),
            @Result(property = "collections", column = "project_id", javaType = Long.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.CollectMapper.findCollectByProjectId")),
            @Result(property = "views", column = "project_id", javaType = Long.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.BrowseHistoryMapper.findBrowseHistoryByProjectId")),
    })
    List<Project> selectProjectList();
    @Insert("INSERT INTO cd_project " +
            "(user_id, project_name, description, path, property, visibility, created_at, category_id) " +
            "VALUES " +
            "(#{userId}, #{projectName}, #{description}, #{path}, " +
            "#{property}, #{visibility}, #{createdAt}, #{categoryId})")
    void addProject(Project project);


    @Select ( "SELECT * FROM cd_project WHERE user_id = #{userId} ORDER BY created_at DESC" )
    @Results({
            @Result(property = "projectId", column = "project_id"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "tags", column = "tags"),
            @Result(property = "user", column = "user_id", javaType = String.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.UserMapper.findBolgUserById")),
            @Result(property = "category", column = "category_id", javaType = Category.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.ContentMapper.selectCategoryById")),
            @Result(property = "collections", column = "project_id", javaType = Long.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.CollectMapper.findCollectByProjectId")),
            @Result(property = "views", column = "project_id", javaType = Long.class,
                    one = @One(select = "com.example.itcacp.repository.mybatis.BrowseHistoryMapper.findBrowseHistoryByProjectId")),
    })
    List<Project> selectProjectByUserIdList(Long userId);
}
